import { computed, ref } from 'vue'
import { defineStore } from 'pinia';
import { logoutApi } from '@/api'
export const useUserStore = defineStore('userStore', () => {
	const token = ref('') // token 信息
	const userInfo = ref({ // 用户信息
		uid: '', //ID
		avatar: "", //头像
		avatar_id: '',
		name: "", //昵称
		role_name: '',
		role_id: '',
		username: ''
	})
	const orderType = computed(() => {
		const ghs = [
			{ type: 1, title: '待确认', icon: '/static/icon/pendding.png' },
			{ type: 2, title: '待发货', icon: '/static/icon/pendding.png' },
			{ type: 3, title: '全部发货', icon: '/static/icon/down.png' },
			{ type: 4, title: '分批发货', icon: '/static/icon/down.png' },
			{ type: 5, title: '已拒绝', icon: '/static/icon/error.png' },
			{ type: 8, title: '已完成', icon: '/static/icon/success.png' },
		]
		const ck = [
			{ type: 0, title: '待接收', icon: '/static/icon/pendding.png' },
			{ type: 1, title: '待分配', icon: '/static/icon/pendding.png' },
			{ type: 2, title: '待验收', icon: '/static/icon/error.png' },
			{ type: 3, title: '已验收', icon: '/static/icon/down.png' },
			{ type: 4, title: '已入库', icon: '/static/icon/success.png' },
			// { type: 5, title: '直接入库', icon: '/static/icon/success.png' },
		]
		if(userInfo.value.role_id == 13) return ghs
		else return ck
	})
	const SETUSERINFO = (info)=> {
		userInfo.value = info
		token.value = info.token
	}
	const LOGOUT = () =>{
		SETUSERINFO({})
		token.value = ''
		logoutApi()
		uni.reLaunch({ url: '/pages/login/login' })
	}
	return {
		token,
		userInfo,
		orderType,
		SETUSERINFO,
		LOGOUT
	}
}, {
  // 数据持久化
	persist: {
		storage: {
			getItem: (key) => {
			  return uni.getStorageSync(key)
			},
			setItem: (key, value) => {
			  return uni.setStorageSync(key, value)
			},
		}
	}
})
